package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.LoginDao;
import com.lvwenguo.dao.ProductListDao;
import com.lvwenguo.model.LoginUser;
import com.lvwenguo.model.ProductList;
import com.lvwenguo.service.ServiceInter;
import com.lvwenguo.utils.JdbcUtil;
import com.sun.org.apache.xpath.internal.objects.XObject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

//实现接口
public class ServiceImple implements ServiceInter {
    private Connection connection;
    private JdbcUtil jdbcUtil;
    private LoginDao loginDao;
    private ProductListDao productListDao;
    public ServiceImple(){
       connection=JdbcUtil.getJdbcUtil().getConnertion();
       loginDao=new LoginDao(connection);
       productListDao=new ProductListDao(connection);
    }
    /*
    * 查询用户数据信息
    * */
    @Override
    public List<LoginUser> selectUser(LoginUser loginUser) {

        String sql="select u.login_account,u.`password`,r.role_name,u.imgurl from sys_user u,sys_role r where u.role_id=r.id and u.login_account=? and u.`password`=? and u.deleted=0";
        Object[] objects={loginUser.getLogin_account(),loginUser.getPassword()};
        try {
            return loginDao.selectList(sql,objects,LoginUser.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


//查询商品列表数据
    @Override
    public List<ProductList> selectProductList() {
        String sql="SELECT p.bar_code,c.category_name,p.product_name,p.sale_price,s.supplier_name FROM biz_product p LEFT JOIN biz_category c ON p.category_id = c.id LEFT JOIN biz_supplier s ON p.supplier_id = s.id where p.deleted=0";
        try {
            return productListDao.selectList(sql,ProductList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//回显数据与表单
    @Override
    public ProductList selectProductBar_codeList(String bar_code) {
        String sql="select p.bar_code,c.category_name,p.product_name,p.sale_price,s.supplier_name from biz_product p,biz_supplier s,biz_category c where p.category_id=c.id and p.supplier_id=s.id and p.bar_code=?";
       Object[] objects={bar_code};
        try {
           return productListDao.selectOnt(sql,objects,ProductList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //修改商品数据
    @Override
    public int updateProduct(ProductList productList) {
        String sql="update biz_product set bar_code=?,product_name=?,sale_price=? where bar_code=?";
        Object[] objects={productList.getBar_code(),productList.getProduct_name(),productList.getSale_price(),productList.getBar_code()};
        try {
            return productListDao.update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
//逻辑删除商品
    @Override
    public int deleteProduct(String bar_code) {
        String sql="update biz_product set deleted=1 where bar_code=?";
        Object[] objects={bar_code};
        try {
            return productListDao.update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<ProductList> selectProductList(String product_name) {
        String sql="SELECT p.bar_code,c.category_name,p.product_name,p.sale_price,s.supplier_name FROM biz_product p LEFT JOIN biz_category c ON p.category_id = c.id LEFT JOIN biz_supplier s ON p.supplier_id = s.id where p.deleted=0 and p.product_name like ?";
        Object[] objects={product_name};
        try {
           return productListDao.selectList(sql,objects,ProductList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
